import 'package:json_annotation/json_annotation.dart';
import 'package:objectbox/objectbox.dart';
import 'package:xsdk_flutter/xsdk/xsdk_flutter_package.dart';

//数据库生成: flutter pub run build_runner build
//定义生成的文件 flutter pub run build_runner build --delete-conflicting-outputs
part 'models.g.dart';

//商品
@JsonSerializable(explicitToJson: true)
@Entity()
class BeanOfflineDBGood {
  @Id()
  int id=0;

  String? itemTypefCode;
  String? itemId;
  String? specId;
  String? itemCode;
  String? itemName;
  String? specName;
  String? specType;
  String? skuBarcode;
  String? attachBarcode;
  String? pinyin;//拼音首字母
  String? json;
  String? jsonSimple;

  int version=0;//数据版本号

  bool isDel=false;

  int lineId;// 用于排序

  int shelfValidityStart=0;//上架有效期开始时间
  int shelfValidityEnd=0;//上架有效期结束时间

  final shelfTime=ToMany<BeanShelfTime>();

  BeanOfflineDBGood(
      {
        this.id=0,
        this.version=0,
        this.itemTypefCode,
        this.itemId,
        this.itemCode,
        this.itemName,
        this.specType,
        this.specId,
        this.specName,
        this.skuBarcode,
        this.attachBarcode,
        this.pinyin,
        this.json,
        this.jsonSimple,
        this.isDel=false,
        this.lineId=0,
        this.shelfValidityStart=0,
        this.shelfValidityEnd=0,
      }
      );

  //添加固定格式
  factory BeanOfflineDBGood.fromJson(Map<String, dynamic> json) => _$BeanOfflineDBGoodFromJson(json);
  Map<String, dynamic> toJson() => _$BeanOfflineDBGoodToJson(this);

}

//时间段
@JsonSerializable(explicitToJson: true)
@Entity()
class BeanShelfTime {
  @Id()
  int id=0;

  int shelfTimeStart=0;//上架有效期时间段
  int shelfTimeEnd=0;//上架有效期时间段

  BeanShelfTime(
      {
        this.id=0,
        this.shelfTimeStart=0,
        this.shelfTimeEnd=0,
      }
      );


  //添加固定格式
  factory BeanShelfTime.fromJson(Map<String, dynamic> json) => _$BeanShelfTimeFromJson(json);
  Map<String, dynamic> toJson() => _$BeanShelfTimeToJson(this);

}

//商品规格
@JsonSerializable(explicitToJson: true)
@Entity()
class BeanOfflineDBGoodSpec {
  @Id()
  int id=0;
  String? itemId;
  String? specId;
  String? specName;
  String? skuBarcode;
  String? attachBarcode;
  String? json;
  int version=0;//数据版本号
  bool isDel;

  BeanOfflineDBGoodSpec(
      {
        this.id=0,
        this.version=0,
        this.itemId,
        this.specId,
        this.specName,
        this.json,
        this.skuBarcode,
        this.attachBarcode,
        this.isDel=false,
      }
      );
  //添加固定格式
  factory BeanOfflineDBGoodSpec.fromJson(Map<String, dynamic> json) => _$BeanOfflineDBGoodSpecFromJson(json);
  Map<String, dynamic> toJson() => _$BeanOfflineDBGoodSpecToJson(this);

}

//订单
@JsonSerializable(explicitToJson: true)
@Entity()
class BeanOfflineDBOrder {
  @Id()
  int id=0;
  String? orderId;
  String? orderNo;
  String? orderDate;
  String? json;


  BeanOfflineDBOrder(
      {
        this.id=0, this.orderId, this.orderNo, this.orderDate, this.json
      });

  //添加固定格式
  factory BeanOfflineDBOrder.fromJson(Map<String, dynamic> json) => _$BeanOfflineDBOrderFromJson(json);
  Map<String, dynamic> toJson() => _$BeanOfflineDBOrderToJson(this);

}

//挂单
@JsonSerializable(explicitToJson: true)
@Entity()
class BeanOfflineDBPendingOrder {
  @Id()
  int id=0;
  int? serialNumber;//流水号
  String? creatDate;
  String? totalMoney;
  String? json;
  String? items;


  BeanOfflineDBPendingOrder({this.id=0, this.serialNumber, this.creatDate,
    this.totalMoney, this.json,this.items});

  //添加固定格式
  factory BeanOfflineDBPendingOrder.fromJson(Map<String, dynamic> json) => _$BeanOfflineDBPendingOrderFromJson(json);
  Map<String, dynamic> toJson() => _$BeanOfflineDBPendingOrderToJson(this);

}




///////////////////////////////////////////////////

//商品
@JsonSerializable(explicitToJson: true,includeIfNull: false)
@Entity()
class BeanShoppingCartDBGood {
  @Id()
  int xid = 0;

  String? xTag;//模块

  @JsonKey(readValue: _readValue)
  String? specType; //0,1
  @JsonKey(readValue: _readValue)
  String? isUniqueCode; //api的isUniqueCode字段返回2种 int string
  @JsonKey(readValue: _readValue)
  String? uniqueCodeType;
  @JsonKey(readValue: _readValue)
  String? ruleType;
  @JsonKey(readValue: _readValue)
  String? bomType;
  @JsonKey(readValue: _readValue)
  String? batchType;
  @JsonKey(readValue: _readValue)
  String? isBatch;
  @JsonKey(readValue: _readValue)
  String? isBom; //1 //是否bom
  @JsonKey(readValue: _readValue)
  static String? _readValue(Map<dynamic,dynamic> json,String key){
    return '${json[key]}';
  }



  String? bomId;
  String? itemId;
  String? specId;

  String? imageUrlFull;
  double? dealPrice;
  String? unit;
  String? itemUnit;
  bool userTagIsContainZero;//数量为0是否保留

  double? quantity;
  double? itemQuantity;

  double? currentPrice; //当不使用此字段时,计算自动使用dealPrice替代
  String? currentUnit;
  double currentQuantity = 0; //
  double currentBaseQuantity = 0; // 多规格时，此数量应该为总数量
  double? maxAvailableQuantity; // 最大可选数量

  String? remark;
  String? orderItemId;


  //库存
  @JsonKey(readValue: _readValueInvQty)
  double? invQty;
  static double? _readValueInvQty(Map<dynamic,dynamic> json,String key){
    return XNumberUtils.dynamic2double(json[key]);
  }

  //单位转换率
  @JsonKey(readValue: _readValueCurrentUnitRate)
  double? currentUnitRate;
  static double? _readValueCurrentUnitRate(Map<dynamic,dynamic> json,String key){
    return json[key]??XNumberUtils.dynamic2double(json['unitRate']??1.0);
  }

  // 如果是多规格时，此数量应该为总金额
  @JsonKey(readValue: _readValueCurrentMoney)
  double? currentMoney;
  static double? _readValueCurrentMoney(Map<dynamic,dynamic> json,String key){
    double currentQuantity=json['currentQuantity']??0.0;
    double currentPrice=json['currentPrice']??(json['dealPrice']??0.0);
    //已外面的值为准(包含优惠),否则自动重新计算(数量*价格)
    return json[key]??XNumberUtils.d2d(2, currentQuantity * currentPrice);
  }

  //多单位
  @JsonKey(readValue: _readValueUnitListJson)
  String? unitListJson;
  static String? _readValueUnitListJson(Map<dynamic,dynamic> json,String key){
    return XJsonUtils.obj2Json(json['unitList']);
  }

  //唯一码
  @JsonKey(readValue: _readValueUniqueCodeListJson)
  String? uniqueCodeListJson;
  static String? _readValueUniqueCodeListJson(Map<dynamic,dynamic> json,String key){
    return XJsonUtils.obj2Json(json['currentUniqueCodeList']);
  }

  //批次码
  @JsonKey(readValue: _readValueBatchCodeListJson)
  String? batchCodeListJson;
  static String? _readValueBatchCodeListJson(Map<dynamic,dynamic> json,String key){
    return XJsonUtils.obj2Json(json['currentBatchCodeList']);
  }


  //店内加工
  String? adjustType;

  //costPrice
  double? costPrice;

  @JsonKey(readValue: _readValueAdjustTypeListJson)
  String? adjustTypeListJson;
  static String? _readValueAdjustTypeListJson(Map<dynamic,dynamic> json,String key){
    return XJsonUtils.obj2Json(json['unitList']);
  }


  String? itemTypefCode;
  String? itemCode;
  String? itemName;
  String? specName;
  String? skuBarcode;
  String? pinyin; //拼音首字母

  @JsonKey(readValue: _readValueJsonData)
  String? jsonData;
  static String? _readValueJsonData(Map<dynamic,dynamic> json,String key){
    return XJsonUtils.obj2Json(json['jsonData']);
  }

  BeanShoppingCartDBGood({
    this.xid = 0,
    this.xTag,
    this.specType,
    this.bomType,
    this.isBom,
    this.batchType,
    this.isBatch,
    this.userTagIsContainZero = false,
    this.isUniqueCode,
    this.uniqueCodeType,
    this.currentQuantity = 0.0,
    this.currentBaseQuantity = 0.0,
    this.maxAvailableQuantity,
    this.currentMoney,
    this.currentUnitRate,
    this.bomId,
    this.itemId,
    this.itemName,
    this.dealPrice,
    this.skuBarcode,
    this.specId,
    this.specName,
    this.unit,
    this.currentUnit,
    this.invQty,
    this.currentPrice,
    this.unitListJson,
    this.uniqueCodeListJson,
    this.batchCodeListJson,
    this.remark,
    this.orderItemId,
    this.adjustType,
    this.costPrice,
    this.adjustTypeListJson,
    this.jsonData,
  }) {
    pinyin = XPinyinUtils.getShortPinyin(itemName ?? "");
  }

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
          other is BeanShoppingCartDBGood && runtimeType == other.runtimeType && itemId == other.itemId;

  @override
  int get hashCode => itemId.hashCode;

  //添加固定格式
  factory BeanShoppingCartDBGood.fromJson(Map<String, dynamic> json) =>
      _$BeanShoppingCartDBGoodFromJson(json);

  Map<String, dynamic> toJson() => _$BeanShoppingCartDBGoodToJson(this);
}

//商品规格
@JsonSerializable(explicitToJson: true,includeIfNull: false)
@Entity()
class BeanShoppingCartDBGoodSpec{
  @Id()
  int xid = 0;

  String? xTag; //模块
  String? bomId;
  String? itemId;
  String? specId;

  String? itemName;
  String? specName;
  String? skuBarcode;
  String? itemCode;

  String? imageUrlFull;
  double? dealPrice;
  String? unit;
  String? remark;
  String? orderItemId;

  @JsonKey(readValue: _readValue)
  String? specType; //0,1
  @JsonKey(readValue: _readValue)
  String? isUniqueCode; //api的isUniqueCode字段返回2种 int string
  @JsonKey(readValue: _readValue)
  String? uniqueCodeType;
  @JsonKey(readValue: _readValue)
  String? ruleType;
  @JsonKey(readValue: _readValue)
  String? bomType;
  @JsonKey(readValue: _readValue)
  String? batchType;
  @JsonKey(readValue: _readValue)
  String? isBatch;
  @JsonKey(readValue: _readValue)
  String? isBom; //1 //是否bom
  @JsonKey(readValue: _readValue)
  String? invQty;

  static String? _readValue(Map<dynamic,dynamic> json,String key){
    return '${json[key]}';
  }

  double? currentPrice; //当不使用此字段时,计算自动使用dealPrice替代
  String? currentUnit;
  double currentQuantity;
  double currentBaseQuantity;
  double? maxAvailableQuantity; // 最大可选数量


  @JsonKey(readValue: _readValueJsonData)
  String? jsonData;
  static String? _readValueJsonData(Map<dynamic,dynamic> json,String key){
    return XJsonUtils.obj2Json(json[key]);
  }


  //单位转换率
  @JsonKey(readValue: _readValueCurrentUnitRate)
  double? currentUnitRate;
  static double? _readValueCurrentUnitRate(Map<dynamic,dynamic> json,String key){
    return json[key]??(json['unitRate']??1.0);
  }

  //如果是多规格时，此数量应该为总金额
  @JsonKey(readValue: _readValueCurrentMoney)
  double? currentMoney;
  static double? _readValueCurrentMoney(Map<dynamic,dynamic> json,String key){
    double currentQuantity=json['currentQuantity']??0.0;
    double currentPrice=json['currentPrice']??(json['dealPrice']??0.0);
    //已外面的值为准(包含优惠),否则自动重新计算(数量*价格)
    return json[key]??XNumberUtils.d2d(2, currentQuantity * currentPrice);
  }

  //多单位
  @JsonKey(readValue: _readValueUnitListJson)
  String? unitListJson;
  static String? _readValueUnitListJson(Map<dynamic,dynamic> json,String key){
    return XJsonUtils.obj2Json(json['unitList']);
  }

  //唯一码
  @JsonKey(readValue: _readValueUniqueCodeListJson)
  String? uniqueCodeListJson;
  static String? _readValueUniqueCodeListJson(Map<dynamic,dynamic> json,String key){
    return XJsonUtils.obj2Json(json['currentUniqueCodeList']);
  }

  //批次码
  @JsonKey(readValue: _readValueBatchCodeListJson)
  String? batchCodeListJson;
  static String? _readValueBatchCodeListJson(Map<dynamic,dynamic> json,String key){
    return XJsonUtils.obj2Json(json['currentBatchCodeList']);
  }

  //店内加工
  String? adjustType;

  //成本价
  double? costPrice;


  @JsonKey(readValue: _readValueAdjustTypeListJson)
  String? adjustTypeListJson;
  static String? _readValueAdjustTypeListJson(Map<dynamic,dynamic> json,String key){
    return XJsonUtils.obj2Json(json['unitList']);
  }

  BeanShoppingCartDBGoodSpec({
    this.xid = 0,
    this.xTag,
    this.bomType,
    this.batchType,
    this.isBom,
    this.isBatch,
    this.uniqueCodeType,
    this.isUniqueCode,
    this.bomId,
    this.itemId,
    this.itemName,
    this.specId,
    this.specName,
    this.currentQuantity = 0.0,
    this.currentBaseQuantity = 0.0,
    this.maxAvailableQuantity,
    this.currentMoney,
    this.dealPrice,
    this.currentUnitRate,
    this.unit,
    this.currentUnit,
    this.invQty,
    this.currentPrice,
    this.unitListJson,
    this.uniqueCodeListJson,
    this.batchCodeListJson,
    this.remark,
    this.orderItemId,
    this.adjustType,
    this.costPrice,
    this.adjustTypeListJson,
  });

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
          other is BeanShoppingCartDBGoodSpec &&
              runtimeType == other.runtimeType &&
              itemId == other.itemId &&
              specId == other.specId;

  @override
  int get hashCode => itemId.hashCode ^ specId.hashCode;

  //添加固定格式
  factory BeanShoppingCartDBGoodSpec.fromJson(Map<String, dynamic> json) =>
      _$BeanShoppingCartDBGoodSpecFromJson(json);

  Map<String, dynamic> toJson() => _$BeanShoppingCartDBGoodSpecToJson(this);

  static Object? _uniqueCodeTypeReadValue(Map<dynamic, dynamic> json,
      String a) {
    return '${json['d']}';
  }

}

///////////////////////////////////////////////////

@Entity()
class TestDBItem {
  @Id()
  int id=0;
  String name;
  TestDBItem({required this.name});
}